﻿using cpe.cpein.UI.UserCpeInServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Gs = cpe.cpein.UI.GroupCpeInServiceReference;


namespace cpe.cpein.UI.Controls
{
    /// <summary>
    /// Interaction logic for UCAddGroup.xaml
    /// </summary>
    public partial class UCAddGroup : UserControl
    {
        #region Variables declaration
        /// <summary>
        /// Connection to WebService Group
        /// </summary>
        UsersServiceClient userService = new UsersServiceClient();

        /// <summary>
        /// Current user.
        /// </summary>
        User _currentUser;

        /// <summary>
        /// List which have to fill the comb box
        /// </summary>
        List<String> cmbBoxGroupTypeSource= new List<string>();

        /// <summary>
        /// Group UserControl
        /// </summary>
        UCListGroups _ucListGroup;

        /// <summary>
        /// Current Main window
        /// </summary>
        FMainWindow _WinParent;

        /// <summary>
        /// Group Types
        /// </summary>
        private List<Gs.GroupType> _groupTypes = new List<Gs.GroupType>();
        public List<Gs.GroupType> MyGroupTypes
        {
            get { return _groupTypes; }
            set { _groupTypes = value; }
        }

        #endregion

        #region Create and Open

        /// <summary>
        /// Default constructor
        /// </summary>
        public UCAddGroup()
        {
            InitializeComponent();
            InitializeForm();
        }

        public UCAddGroup(User u, FMainWindow WinParent)
        {
            this._currentUser = u;
            this._WinParent = WinParent;
            InitializeComponent();
            InitializeForm();
        }



        #endregion

        #region Events methods
        #endregion

        #region Methods

        /// <summary>
        /// Fill the form
        /// </summary>
        private void InitializeForm()
        {
            Tools.Tools.BeginWaitStatus();

            List<Gs.GroupType> grt = GetGroupTypes();

            this.MyGroupTypes.Clear();
            this.MyGroupTypes.AddRange(grt);
            this.cmbBoxType.ItemsSource = grt;

            Tools.Tools.EndWaitStatus();
        }
        
        /// <summary>
        /// Function used to get the group types.
        /// </summary>
        private List<Gs.GroupType> GetGroupTypes()
        {
            using (Gs.GroupsServiceClient gs = new Gs.GroupsServiceClient())
            {
                return new List<Gs.GroupType>(gs.GetAllGroupTypes(
                    new Gs.User()
                    {
                        Email = this._currentUser.Email,
                        Id = this._currentUser.Id,
                        IDAddress = this._currentUser.IDAddress,
                        IsDeleted = this._currentUser.IsDeleted,
                        Lastname = this._currentUser.Lastname,
                        Name = this._currentUser.Name,
                    }));
            }
        }

        #endregion

        /// <summary>
        /// Fires when cancel button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            if (_ucListGroup == null)
                _ucListGroup = new UCListGroups(_currentUser,_WinParent);
            LoadUserControl(_ucListGroup);
        }

        /// <summary>
        /// Function which allows to load a UserControl in the form.
        /// </summary>
        /// <param name="uc"></param>
        private void LoadUserControl(UserControl uc)
        {
            _WinParent.ucContent.Content = uc;
        }

        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            if ((txtGroupName.Text != null) && (this.cmbBoxType.SelectedValue!=null))
            {
                Group groupToAdd = new Group();

                groupToAdd.IDType = int.Parse(this.cmbBoxType.SelectedValue.ToString());
                groupToAdd.Label = txtGroupName.Text;
                groupToAdd.IsDeleted = false;
                if (ChkBoxIsWorkable.IsChecked == true)
                    groupToAdd.IsWorkable = true;
                else
                    groupToAdd.IsWorkable = false;

                groupToAdd.Description = TxtGroupDesc.Text;
                this.userService.AddGroup(groupToAdd, _currentUser);

                if (_ucListGroup == null)
                    _ucListGroup = new UCListGroups(_currentUser, _WinParent);
                LoadUserControl(_ucListGroup);
            }
        }
        
    }
}
